/*
* @file model/accoutuser.js
*  用户管理
* @author
*
*/
import { routerRedux } from 'dva/router';
import api from '../api';
import moment from 'moment';
import { notification } from 'antd';
import { delay } from '../utils/sagaEffects';
export default {
	namespace: 'accoutuser',
	state:{
		userlist:[]
	},
	subscriptions:{
		setup({dispatch,history}) {
			history.listen(({pathname,query}) => {
				if( pathname === '/systemMgt/accountMgt/userMgt' ){
					dispatch({
						type: 'getUsersList',
						payload: query
					})
				}
			})
		}
	},
	effects:{
		*changeRoute({payload:query},{call,put}){
			yield put(routerRedux.push({
				pathname:`/systemMgt/accountMgt/userMgt`,
				query:{
					...query,
				}
			}))
		},
		*getRoles({payload:query},{call,put}){
			const response = yield call(api.role.getRoles, query);
			yield put({
				type: 'getRolesSuccess',
				payload: response ,
			})
		},
		* getUsersList({ payload:query }, { call, put }) {
			yield put({type:'clearState'})
				yield put({
					type: 'initData',
					payload:{
						userlist:[]
					}
			});
				const response = yield call(api.getUsersInfo, query);
				yield put({
					type: 'getListSuccess',
					payload: response ,
				})
		},
		* getUsersAdd({ payload }, { call, put }) {
				const response = yield call(api.getUsersAdd, payload );
					yield put({
						type: 'addSuccess',
						payload: { response },
				});
				yield put({
					type: 'getUsersList',
				});
		},
		* gotoEdit({payload}, {call, put}){
			yield put(routerRedux.push({
				pathname:`/systemMgt/accountMgt/userMgt/revise`,
				payload: {
					name:query.username,
				}
			}))
		},
		* gotopermission({payload}, {call, put}){
			const username = payload.username
			yield put(routerRedux.push({
				pathname:`/systemMgt/accountMgt/userMgt/permission`,
				query:{
					username
				}
			}))
		},
		* saveUsrsEdit({ payload }, { call, put }) {
			const username = payload.username
			const response = yield call(api.saveUsrsEdit,username, { ...payload });
			yield put({
				type: 'saveSuccess',
				payload: { response },
			});
		},
	},
	reducers:{
		initData(state,{payload}){
			return{
				...state,
				...payload
			}
		},
		clearState(state) {
			return{}
		},
		getListSuccess(state, {payload}) {
			const {data,page} = payload
			data.map((item)=>{
				const year = item.createDate.substring(0,4)
				const month = item.createDate.substring(4,6)
				const day = item.createDate.substring(6,8)
				item.createDate = year + '-' + month + '-' + day
			})
			const status=data ? true:false
			return {
				...state,
				status,
				userlist:data,
				page,
			}
		},
		getRolesSuccess(state,{payload}){
			const {data} = payload;
			data.forEach(function(item){
		    item.label = item.roleName;
		    item.value = item.roleCode;
		    delete item.roleName;
		    delete item.roleCode;
		    delete item.roleDescription;
			})
			return {
				...state,
				roleslist:data,
			}
		},
		addSuccess(state, action) {
		const { payload: { response } } = action;
		return {
		...state,
			};
		},
		saveSuccess(state, action) {
			const { payload: { response } } = action;
			notification['success']({
		   message: '保存成功',
		  });
			return {
				...state,
			};
		},
	}

}
